package com.nldyp.movie.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.nldyp.movie.config.AlipayConfig;
import com.nldyp.movie.model.dto.PayMentDto;
import com.nldyp.movie.model.dto.PayRefundDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(tags="支付宝支付接口")
@RequestMapping("/alipay")
@RestController
public class MovieDataController {

    @ApiOperation(value = "支付宝支付接口")
    @PostMapping(value = "/payment", produces = "application/json; UTF-8")
    public String payMent(@RequestBody PayMentDto payMentDto)throws Exception{
        AlipayClient client = new DefaultAlipayClient(
                AlipayConfig.URL,
                AlipayConfig.APPID,
                AlipayConfig.RSA_PRIVATE_KEY,
                AlipayConfig.FORMAT,
                AlipayConfig.CHARSET,
                AlipayConfig.ALIPAY_PUBLIC_KEY,
                AlipayConfig.SIGNTYPE);//new DefaultAlipayClient(AlipayConfig.getClientParams());
        JSONObject bizContent = new JSONObject();
        bizContent.set("out_trade_no", payMentDto.getOutTradeNo());
        bizContent.set("total_amount", payMentDto.getTotalAmount());
        bizContent.set("subject", payMentDto.getSubject());
        bizContent.set("product_code", "FAST_INSTANT_TRADE_PAY");
        AlipayTradePayRequest request = new AlipayTradePayRequest();

        request.setBizContent(bizContent.toString());

        System.out.println("订单支付入参："+bizContent.toString());
        AlipayTradePayResponse response = client.pageExecute(request,"GET");
        System.out.println("订单支付出参："+response.getBody());

        return response.getBody();
    }

    @ApiOperation(value = "支付宝查询接口")
    @GetMapping(value = "/payquery", produces = "application/json; UTF-8")
    public String payQuery(@RequestParam(value = "outTradeNo", required = true) String outTradeNo)throws Exception{
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.getClientParams());
        // 创建API对应的request类
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject json = JSONUtil.createObj();
        // 设置业务参数
        json.put("out_trade_no", outTradeNo);
        request.setBizContent(json.toString());
        System.out.println(outTradeNo+"查询订单支付情况入参："+json.toString());
        // 通过alipayClient调用API，获得对应的response类
        AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);
        System.err.println(outTradeNo+"查询订单支付情况出参："+response.getBody());
        JSONObject resultJO = JSONUtil.parseObj(response.getBody());
        resultJO = resultJO.getJSONObject("alipay_trade_query_response");
        return resultJO.toString();
    }

    @ApiOperation(value = "支付宝退款接口")
    @PostMapping(value = "/payrefund", produces = "application/json; UTF-8")
    public String payRefund(@RequestBody PayRefundDto payRefundDto)throws Exception{
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.getClientParams());
        JSONObject bizContent = new JSONObject();
        bizContent.set("trade_no", payRefundDto.getTradeNo());
        bizContent.set("out_trade_no", payRefundDto.getOutTradeNo());
        bizContent.set("refund_amount", payRefundDto.getRefundAmount());
        bizContent.set("out_request_no", payRefundDto.getOutRequestNo());
        AlipayTradePayRequest request = new AlipayTradePayRequest();

        request.setBizContent(bizContent.toString());

        System.out.println("订单退款入参："+bizContent.toString());
        AlipayTradePayResponse response = client.certificateExecute(request);
        System.out.println("订单退款出参："+response.getBody());
        JSONObject resultJo = JSONUtil.parseObj(response.getBody());
        resultJo = resultJo.getJSONObject("alipay_trade_refund_response");
        return resultJo.toString();
    }
}
